package com.webserver.controller;

import com.webserver.core.DispatcherServlet;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;

/**
 * 处理与用户相关的业务操作
 */
public class UserController {
    private static File userDir;
    private static File root;
    private static File staticDir;

    static {
        try {
            root = new File(DispatcherServlet.class.getClassLoader().getResource(".").toURI());
            staticDir = new File(root,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        userDir = new File("./users");
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }

    public void reg(HttpServletRequest request , HttpServletResponse response){
        //1 获取用户在注册页面上输入的注册信息,获取from表单提交的内容
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");

        /*
            必要的验证，要求：
            四项信息不能为null，并且年龄必须是一个数字(正则表达式)
            否则直接给用户一个注册失败的页面：reg_input_error.html
            该页面居中显示一行字：输入信息有误，注册失败。
         */
        if (username==null||password==null||
                nickname==null||ageStr==null
                ||!ageStr.matches("[0-9]+")
        ){
            File file = new File(staticDir,"/myweb/reg_input_error.html");
            response.setContentFile(file);
            return;
        }

            int age = Integer.parseInt(ageStr);

            System.out.println(username + "," + password + "," + nickname + "," + ageStr);
            //2 将用户信息保存
            File userFile = new File(userDir, username + ".obj");
            if (userFile.exists()){
                File file = new File(staticDir,"myweb/have_user.html");
                response.setContentFile(file);
                return;
            }
            try (
                    FileOutputStream fos = new FileOutputStream(userFile);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
            ) {
                User user = new User(username, password, nickname, age);
                oos.writeObject(user);

                //注册成功了
                File file = new File(staticDir, "/myweb/reg_success.html");
                response.setContentFile(file);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //3 给用户响应一个注册结果页面(注册成功或注册失败)

}
